package vn.com.epi.utils;

import java.util.List;

public class StringUtils {
    
    public static boolean validInteger(String integer) {
        try {
            Integer.parseInt(integer);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
    
    public static String arrayToString(String[] array) {
        if (array.length == 0)
            return null;
        else {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < array.length; i++)
                sb.append(array[i]).append(",");
            deleteLastChar(sb);
            return sb.toString();
        }
    }

    public static int getNumberOfItems(String string) {
        try {
            return string.split(",").length;
        } catch (Exception e) {
            return 0;
        }
    }
    
    public static void insertCommaChar(StringBuilder sb) {
        if (sb.length() > 0)
            sb.append(",");
    }
    
    public static void deleteLastChar(StringBuilder sb) {
        if (sb.length() > 0)
            if (sb.charAt(sb.length() - 1) == ',')
                sb.deleteCharAt(sb.length() - 1);
    }       
    
    public static String listToString(List<Integer> list) {
        StringBuilder sb = new StringBuilder();
        for (int element: list) 
            sb.append(element).append(",");
        deleteLastChar(sb);
        return sb.toString();
        
    }
    
    public long memoryUsage() {
        long usableFreeMemory = Runtime.getRuntime().maxMemory()
                - Runtime.getRuntime().totalMemory()
                + Runtime.getRuntime().freeMemory();
        return usableFreeMemory;
    }
    
}
